/************************************************************
 * @file: nvsns_init.c
 * @author: Hao
 * @version: V0.0
 * @data: 2023/07/28
 * @brief: nvsns module initilization file
 * @note:
 * @Copyright (C) 2023 Novosense All rights reserved.
 ************************************************************/

/************************************************************
 ************************************************************
 * @par Edition History
 * -V0.0  2023.07.28
 *        - Initilization file
 *
 ************************************************************/

#include "nvsns_init.h"
#include "peripheral_config.h"

/************************************************************
 * @brief: Initialize EPWM module
 * @return <None>
 ************************************************************/
void EPWM_Init(void) 
{
    /* Enable EPWM & Chargepump */
    SYSCTRL->AHBCGR_b.PWMCEN = 1;
    SYSCTRL->HCR_b.CE      = 1;
//    SYSCTRL->ANACR_b.ISNGC = 3;// DAC控制满量程电流1400mA

    EPWM->CMP0 = 0;            // set compoare value to 0
    EPWM->CMP2 = 0;

    EPWM->PER0 = EPWM_PERIOD;  // set pwm period
    EPWM->PER2 = EPWM_PERIOD;

    EPWM->CR2_b.PRBE0 = 1;     // enable register buffer
    EPWM->CR2_b.PRBE2 = 1;
    EPWM->CR2_b.PRUM0 = 0;     // PERx, COMPx, TRGx register update on
    EPWM->CR2_b.PRUM2 = 0;     // 0: update request(PURx) 1: without

    EPWM->CR1 = 0x0E0E;             // MOUT HS/LS mux to constaint low

    EPWM->CR0_b.FSON  = EPWM_FSON;   // set switch on slew rate
    EPWM->CR0_b.FSOFF = EPWM_FSOFF;  // set switch off slew rate
    EPWM->CR0_b.CCPT  = 0;           // set cross-current to 0
    EPWM->CR0_b.CKSEL = EPWM_FSEL;   // select fpwm 32MHz
    EPWM->CR0_b.OCFT  = EPWM_OCFT;   // select over-current filter time   
                                     // 0: 2.4us - 2.8 us       1: 4.8 us - 5.6 us 
                                     // 2: 9.6us - 11.2 us      3: 19.2 us - 22.4 us
    EPWM->CR0_b.BCM = 0;             // set bridge mode: 0:paired 1:single
    EPWM->CR0_b.PAM = EPWM_PAM;      // set alignment:0:center 1:zero

    /*Disable EPWM0 rising trig event*/
    EPWM->TCR_b.UCE0 = 0;
    /*enable EPWM0 falling trig event*/
    EPWM->TCR_b.DCE0 = 1;
    /*trig timing setting*/
    EPWM->TRG0 = 0;      // trig time in the middle of PWM ON for center aligned

    EPWM->CR0_b.EN = 0;  // disable EPWM connect to MOUT
}

/************************************************************
 * @brief: Initialize ADC module
 * @return <None>
 ************************************************************/
void ADC_Init(void) 
{
    /*enable adc clk*/
    SYSCTRL->AHBCGR_b.ADCCEN = 1;

    /* config ADC */
    ADC->CR_b.TSRC  = 1;  // EPWM0 Trig
    ADC->CR_b.CKSEL = 0;  // ADC CLK = 32Mhz
    
	/* GPIO1 模拟输入ADC引脚 */
    GPIO->MXR_b.PM1 = 7;

//	ADC->WCR0_b.WCOEF  = 0x00000278;
//	ADC->WCR1_b.WCOEF  = 0x000010A8;
//	ADC->WCR2_b.WCOEF  = 0x0000164C;
//	ADC->WCR3_b.WCOEF  = 0x00002D84;
//	ADC->WCR4_b.WCOEF  = 0x0000595D;
//	ADC->WCR5_b.WCOEF  = 0x0000A743;
//	ADC->WCR6_b.WCOEF  = 0x00013900;
//	ADC->WCR7_b.WCOEF  = 0x00023706;
//	ADC->WCR8_b.WCOEF  = 0x00034761;
//	ADC->WCR9_b.WCOEF  = 0x000632B3;
//	ADC->WCR10_b.WCOEF = 0x000B2962;
//	ADC->WCR11_b.WCOEF = 0x00147B89;
//	ADC->WCR12_b.WCOEF = 0x0025A604;
//	ADC->WCR13_b.WCOEF = 0x0045128B;
//	
//	ADC->WCR14_b.WCOEF = 0X007F0F1B;
//    ADC->WCR15_b.WCOEF = 0X00E9C087;
    /* config delay stable convert sample time */
    /* !!!ATTENTIOIN: changing this value is not recommand */
//    ADC->SCR_b.ONDLY  = 0x05; // 触发到启动转换之间的延时 (ONDLY[5:0] + 1) / fsys
//    ADC->SCR_b.STABLE = 0x10; // 转换结束到下次转换之间的延时 (STABLE[5:0] + 1) / fsys
//    ADC->SCR_b.CONV   = 0x13; // 数据转换时间 (CONV[5:0] + 1) / fsys
//    ADC->SCR_b.SAMP   = 0x35; // 采样保持时间 (SAMP[5:0] + 1) / fsys
	
	//原厂建议
	ADC->SCR_b.ONDLY  = 0x0;  // 触发到启动转换之间的延时 (ONDLY[5:0] + 1) / fsys
    ADC->SCR_b.STABLE = 0xA;  // 转换结束到下次转换之间的延时 (STABLE[5:0] + 1) / fsys
    ADC->SCR_b.CONV   = 0x12; // 数据转换时间 (CONV[5:0] + 1) / fsys
    ADC->SCR_b.SAMP   = 0xA; // 采样保持时间 (SAMP[5:0] + 1) / fsys


    ADC->TCR_b.REFSEL = 0;  // 0:internal Vref  1:external Vref  2:AVDD 
    ADC->TCR_b.SIGEN  = 0;  // 0:不使能单端模式, 转换输出范围0-32767
							// 1:使能单端模式，转换输出范围-32768-32767
							
//	ADC->GTR_b.GAINTRIM_D = 0x232; // ADC最大值：32767 * (0x200/4096) = 4096
//	ADC->GTR_b.GAINTRIM_S = 0x232;
//	ADC->OTR_b.OFFTRIM_D = 0; //差分模式偏置
//	ADC->OTR_b.OFFTRIM_S = 0; //单端模式偏置

    /* sample sequence */
    ADC->QCR1_b.Q1EN  = 1;
    ADC->QCR1_b.Q1SEL = 1;  // 1 = MVSS
    ADC->QCR1_b.Q1G   = 3;  // Gain: 0=1, 1=2, 2=4, 3=10, 4=20, 5=40, 6=4/11, 7=Bypass
    
    ADC->QCR1_b.Q2EN  = 1;
    ADC->QCR1_b.Q2SEL = 5;  // 5 = Temp
    ADC->QCR1_b.Q2G   = 0;  // Gain: 0=1, 1=2, 2=4, 3=10, 4=20, 5=40, 6=4/11, 7=Bypass
	ADC->TPSR_b.TSA = 0x3DA;
	ADC->TPSR_b.TST = 0x556;
	
	ADC->QCR1_b.Q3EN  = 1;  
	ADC->QCR1_b.Q3SEL = 8;  // 8 = LGPIO1
	ADC->QCR1_b.Q3G   = 0;  // Gain: 0=1, 1=2, 2=4, 3=10, 4=20, 5=40, 6=4/11, 7=Bypass
    
    ADC->IEN_b.EOC = 1;     // end of convert interrupt
    ADC->CR_b.EN   = 0;     // disable ADC module
    
    /*set interrupt priority*/
    LL_NVIC_SetPriority(ADC_IRQn, ADC_PREEMPT_PRIO, ADC_IRQ_PRIO);
    LL_NVIC_DisableIRQ(ADC_IRQn);
}


/************************************************************
 * @brief: Enable interrupt
 * @return <None>
 ************************************************************/
void BDC_InitModule(void)
{
    LL_NVIC_EnableIRQ(ADC_IRQn);
    
    EPWM->CR0_b.EN = 1; // enable EPWM
    ADC->CR_b.EN   = 1; // enable ADC
}
